//
//  DeviceFanLightViewController.m
//  LightControl
//
//  Created by CKF on 2025/8/14.
//

#import "DeviceFanLightViewController.h"
#import "CustomSlider.h"
#import "TwoCircleView.h"
@interface DeviceFanLightViewController ()
@property(nonatomic,strong)UIScrollView *scrollview;
@property(nonatomic,strong)TwoCircleView *arcView;
@property(nonatomic,strong)CustomSlider *customSlider;
@property(nonatomic,strong)UIButton *stopButton;
@property(nonatomic,strong)UIButton *addButton;
@property(nonatomic,strong)UIButton *autoButton;
@property(nonatomic,strong)UILabel *infoLabel;
@property(nonatomic,strong)NSMutableArray<UIView *> *dotViews;
@property(nonatomic,strong)NSMutableArray<UIButton *> *buttonArray;
@property(nonatomic,strong)NSMutableArray<UIButton *> *brightButtonArray;
@property(nonatomic,strong)UIView *sliderView;
@property(nonatomic,strong)UIView *lightModeView;
@property(nonatomic,strong)UIView *lightBrightView;
@property(nonatomic,strong)UILabel *speedLabel;
@property(nonatomic,strong)UILabel *modeLabel;
@property(nonatomic,strong)UILabel *brightLabel;
@property(nonatomic,strong)UIView *brightListView;
@end

@implementation DeviceFanLightViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"00:11:22:33:44:55";
    [self initSubViews];
    [self addRightItem];
}

-(void)addRightItem{
    UIButton * rightBtn=[[UIButton alloc] initWithFrame:CGRectMake(0, 0, 44, 44)];
    [rightBtn setImage:[UIImage imageNamed:@"DisplaySettings"] forState:0];
    [rightBtn addTarget:self action:@selector(rightAct) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
 
}

-(void)rightAct{
   
}


-(void)brightBtnAct:(UIButton *)btn{
//    NSInteger tag = btn.tag;
    for (int i=0; i<self.brightButtonArray.count; i++) {
        UIButton *button = [self.brightButtonArray objectAtIndex:i];
        [button setBackgroundColor:[UIColor clearColor]];
    }
    [btn setBackgroundColor:RGB(208, 224, 245)];
}

-(void)fanModeBtnAct:(UIButton *)btn{
    for (int i=0; i<self.buttonArray.count; i++) {
        UIButton *button = [self.buttonArray objectAtIndex:i];
        button.tintColor = RGB(42, 87, 128);
    }
    btn.tintColor = RGB(0, 160, 239);
}

-(void)brigthButtonAct:(UIButton *)btn{
    if (btn.tag == 5) {
        [_stopButton setBackgroundColor:RGB(0, 160, 239)];
        _stopButton.selected = YES;
        [_addButton setBackgroundColor:RGB(235, 235, 235)];
        _addButton.selected = NO;
        [_autoButton setBackgroundColor:RGB(235, 235, 235)];
        _autoButton.selected = NO;
    }else if (btn.tag == 6) {
        [_stopButton setBackgroundColor:RGB(235, 235, 235)];
        _stopButton.selected = NO;
        [_addButton setBackgroundColor:RGB(0, 160, 239)];
        _addButton.selected = YES;
        [_autoButton setBackgroundColor:RGB(235, 235, 235)];
        _autoButton.selected = NO;
    }else if (btn.tag == 7) {
        [_stopButton setBackgroundColor:RGB(235, 235, 235)];
        _stopButton.selected = NO;
        [_addButton setBackgroundColor:RGB(235, 235, 235)];
        _addButton.selected = NO;
        [_autoButton setBackgroundColor:RGB(0, 160, 239)];
        _autoButton.selected = YES;
    }
}

-(void)initSubViews{
   
    [self.view addSubview:self.scrollview];
    
    [self.scrollview addSubview:self.arcView];
    
 
    [self.scrollview addSubview:self.infoLabel];
    [self addModeButtons];
    [self.scrollview addSubview:self.sliderView];
    [self.scrollview addSubview:self.lightBrightView];
    [self.scrollview addSubview:self.lightModeView];
    [self.sliderView addSubview:self.speedLabel];
    [self.sliderView addSubview:self.customSlider];
    // 创建数值点
    [self createDotViews];
    // 更新滑块和点的样式
    [self updateSliderAndDots];
    [self.lightBrightView addSubview:self.brightLabel];
    [self.lightBrightView addSubview:self.brightListView];
    [self.lightModeView addSubview:self.modeLabel];
    [self.lightModeView addSubview:self.stopButton];
    [self.lightModeView addSubview:self.addButton];
    [self.lightModeView addSubview:self.autoButton];
    [self createBrightListViews];
    self.scrollview.contentSize = CGSizeMake(0, CGRectGetMaxY(self.lightModeView.frame)+50);
}

-(void)addModeButtons{

    NSArray *imgArray = @[@"Air",@"sleep",@"Nature",@"Eco",@"Turbo",@"Smart",@"close"];
    NSArray *titleArray = @[@"NORMAL",@"SLEEP",@"NATURE",@"ECO",@"TURBO",@"SMART",@"CLOSE"];
    for (int i = 0; i<7; i++) {
        UIImage *lightImage = [UIImage imageNamed:imgArray[i]];
        UIImage *lImage = [lightImage imageWithRenderingMode:UIImageRenderingModeAlwaysTemplate];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(30+(KScreenWidth-60)/4.0*(i%4), CGRectGetMaxY(self.infoLabel.frame)+(i/4)*85+30, (KScreenWidth-60)/4.0, 40);
//        btn.tintColor = RGB(43, 166, 77);
        btn.tintColor = RGB(42, 87, 128);
        [btn setImage:lImage forState:0];
        btn.tag = i;
        [btn addTarget:self action:@selector(fanModeBtnAct:) forControlEvents:UIControlEventTouchUpInside];
        [self.scrollview addSubview:btn];
        [self.buttonArray addObject:btn];
        
        
        UILabel *modeLabel = [[UILabel alloc]initWithFrame:CGRectMake(CGRectGetMinX(btn.frame),CGRectGetMaxY(btn.frame)+10, (KScreenWidth-60)/4.0, 20)];
        [self.scrollview addSubview:modeLabel];
        modeLabel.font = [UIFont systemFontOfSize:15];
        modeLabel.textColor = RGB(22, 23, 27);
        modeLabel.text = titleArray[i];
        modeLabel.textAlignment = NSTextAlignmentCenter;
    }
}

-(void)createBrightListViews{
    NSArray *imgArray = @[@"dark",@"Brightness",@"white",@"warm",@"neutral"];
    for (int i = 0; i<imgArray.count; i++) {
        UIImage *lightImage = [UIImage imageNamed:imgArray[i]];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake((((CGRectGetWidth(self.brightListView.frame)-4)/5.0)+1)*i, 0, (CGRectGetWidth(self.brightListView.frame)-4)/5.0, CGRectGetHeight(self.brightListView.frame));
        [btn setImage:lightImage forState:0];
        btn.tag = i;
        [btn addTarget:self action:@selector(brightBtnAct:) forControlEvents:UIControlEventTouchUpInside];
        [self.brightListView addSubview:btn];
        [self.brightButtonArray addObject:btn];
        
        if (i<imgArray.count-1) {
            UIView *line = [[UIView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(btn.frame), 0, 1, CGRectGetHeight(self.brightListView.frame))];
            line.backgroundColor = RGB(17, 17, 17);
            [self.brightListView addSubview:line];
        }
       
    }
}


// 2. 实现手势处理方法
- (void)sliderTapped:(UITapGestureRecognizer *)gesture {
    // 获取被点击的 UISlider
    UISlider *slider = (UISlider *)gesture.view;
    
    // 获取点击位置在 slider 坐标系中的坐标
    CGPoint point = [gesture locationInView:slider];
    
    // 计算点击位置占 slider 宽度的百分比
    CGFloat percent = point.x / slider.bounds.size.width;
    
    // 根据百分比计算新值
    float newValue = slider.minimumValue + (slider.maximumValue - slider.minimumValue) * percent;
    
    // 设置滑块值并启用动画
    [slider setValue:round(newValue) animated:YES];
    
    [self updateSliderAndDots];
}

- (void)createDotViews {
    float originX = 4;
    float width = self.customSlider.frame.size.width-14;
    for (int i = 0; i <= 6; i++) {
        CGFloat x = originX + i * (width / 6.0);
        UIView *dotView = [[UIView alloc] initWithFrame:CGRectMake(self.customSlider.frame.origin.x+x, self.customSlider.frame.origin.y + self.customSlider.frame.size.height / 2 - 1.5, 3, 3)];
        dotView.backgroundColor = RGB(167, 169, 174);
        dotView.layer.cornerRadius = 1.5;
        [self.sliderView addSubview:dotView];
        [self.dotViews addObject:dotView];
    }
}

- (void)sliderValueChanged:(UISlider *)slider {
    // 对滑块值进行取整
    slider.value = round(slider.value);
    [self updateSliderAndDots];
}

- (void)updateSliderAndDots {
    // 设置滑块轨迹颜色
    self.customSlider.minimumTrackTintColor = RGB(42, 87, 128);
    self.customSlider.maximumTrackTintColor = RGB(219, 221, 226);
    
    // 更新点的颜色
    NSInteger currentIndex = (NSInteger)self.customSlider.value;
    for (NSInteger i = 0; i < self.dotViews.count; i++) {
        UIView *dotView = self.dotViews[i];
        if (currentIndex == i) {
            dotView.backgroundColor = [UIColor clearColor];
        } else {
            dotView.backgroundColor = RGB(167, 169, 174);
        }
    }
   
}

-(UIButton *)stopButton{
    if (!_stopButton) {
        _stopButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _stopButton.frame = CGRectMake(25, 48, (CGRectGetWidth(self.lightModeView.frame)-80)/3.0, 46);
        [_stopButton setBackgroundColor:RGB(0, 160, 239)];
        [_stopButton setTitle:@"STOP" forState:0];
        _stopButton.layer.cornerRadius = 8;
        _stopButton.layer.masksToBounds = YES;
        _stopButton.tag = 5;
        _stopButton.selected = YES;
        _stopButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_stopButton setTitleColor:RGB(22, 23, 27) forState:0];
        [_stopButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_stopButton addTarget:self action:@selector(brigthButtonAct:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _stopButton;
}

-(UIButton *)addButton{
    if (!_addButton) {
        _addButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _addButton.frame = CGRectMake(40+(CGRectGetWidth(self.lightModeView.frame)-80)/3.0, 48, (CGRectGetWidth(self.lightModeView.frame)-80)/3.0, 46);
        [_addButton setBackgroundColor:RGB(235, 235, 235)];
        _addButton.layer.borderWidth = 1;
        _addButton.layer.borderColor = RGB(0, 160, 239).CGColor;
        [_addButton setTitle:@"ADD" forState:0];
        _addButton.layer.cornerRadius = 8;
        _addButton.layer.masksToBounds = YES;
        _addButton.tag = 6;
        _addButton.selected = NO;
        _addButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_addButton setTitleColor:RGB(22, 23, 27) forState:0];
        [_addButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_addButton addTarget:self action:@selector(brigthButtonAct:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _addButton;
}

-(UIButton *)autoButton{
    if (!_autoButton) {
        _autoButton = [UIButton buttonWithType:UIButtonTypeCustom];
        _autoButton.frame = CGRectMake(55+(CGRectGetWidth(self.lightModeView.frame)-80)/3.0*2, 48, (CGRectGetWidth(self.lightModeView.frame)-80)/3.0, 46);
        [_autoButton setBackgroundColor:RGB(235, 235, 235)];
        _autoButton.layer.borderWidth = 1;
        _autoButton.layer.borderColor = RGB(0, 160, 239).CGColor;
        [_autoButton setTitle:@"AUTO" forState:0];
        _autoButton.layer.cornerRadius = 8;
        _autoButton.layer.masksToBounds = YES;
        _autoButton.tag = 7;
        _autoButton.selected = NO;
        _autoButton.titleLabel.font = [UIFont systemFontOfSize:16];
        [_autoButton setTitleColor:RGB(22, 23, 27) forState:0];
        [_autoButton setTitleColor:[UIColor whiteColor] forState:UIControlStateSelected];
        [_autoButton addTarget:self action:@selector(brigthButtonAct:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _autoButton;
}

-(UILabel *)infoLabel{
    if (!_infoLabel) {
        _infoLabel = [[UILabel alloc]initWithFrame:CGRectMake(40, CGRectGetMaxY(self.arcView.frame)+20, KScreenWidth-80, 26)];
        _infoLabel.font = [UIFont systemFontOfSize:17];
        _infoLabel.textColor = RGB(22, 23, 27);
        _infoLabel.text = LocalString(@"Fan mode");
    }
    return _infoLabel;
}


-(CustomSlider *)customSlider{
    if (!_customSlider) {
        _customSlider = [[CustomSlider alloc] initWithFrame:CGRectMake(35, 66, CGRectGetWidth(self.sliderView.frame) - 70, 20)];
        _customSlider.trackHeight = 6;
        _customSlider.minimumValue = 0;
        _customSlider.maximumValue = 6;
        _customSlider.thumbTintColor = RGB(42, 87, 128);
        _customSlider.value = 0;
        _customSlider.minimumTrackTintColor = RGB(42, 87, 128);
        _customSlider.maximumTrackTintColor =  RGB(219, 221, 226);
        [_customSlider addTarget:self action:@selector(sliderValueChanged:) forControlEvents:UIControlEventValueChanged];
        UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(sliderTapped:)];
        [_customSlider addGestureRecognizer:tapGesture];
    }
    return _customSlider;
}

-(TwoCircleView *)arcView{
    if (!_arcView) {
        _arcView = [[TwoCircleView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenWidth)];
    }
    return _arcView;
}

-(UIScrollView *)scrollview{
    if (!_scrollview) {
        _scrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-mNavBarHeight)];
        _scrollview.backgroundColor = RGB(242, 245, 249);
    }
    return _scrollview;
}

-(NSMutableArray *)buttonArray{
    if (!_buttonArray) {
        _buttonArray = [[NSMutableArray alloc]init];
    }
    return _buttonArray;
}

-(NSMutableArray *)brightButtonArray{
    if (!_brightButtonArray) {
        _brightButtonArray = [[NSMutableArray alloc]init];
    }
    return _brightButtonArray;
}

-(UIView *)sliderView{
    if (!_sliderView) {
        _sliderView = [[UIView alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.infoLabel.frame)+215, KScreenWidth-40, 120)];
        _sliderView.backgroundColor = RGB(239, 240, 249);
        _sliderView.layer.shadowOpacity = 0.5;          // 阴影透明度（0~1）
        _sliderView.layer.shadowColor = [UIColor grayColor].CGColor; // 阴影颜色
        _sliderView.layer.shadowRadius = 1;             // 阴影扩散范围（值越大越模糊）
        _sliderView.layer.shadowOffset = CGSizeMake(0, 1); // 阴影偏移（0,0表示四周均匀）
        _sliderView.layer.masksToBounds = NO;
        _sliderView.layer.cornerRadius = 15;
        _sliderView.clipsToBounds = NO;
    }
    return _sliderView;
}


-(UIView *)lightBrightView{
    if (!_lightBrightView) {
        _lightBrightView = [[UIView alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.sliderView.frame)+30, KScreenWidth-40, 120)];
        _lightBrightView.backgroundColor = RGB(239, 240, 249);
        _lightBrightView.layer.shadowOpacity = 0.5;          // 阴影透明度（0~1）
        _lightBrightView.layer.shadowColor = [UIColor grayColor].CGColor; // 阴影颜色
        _lightBrightView.layer.shadowRadius = 1;             // 阴影扩散范围（值越大越模糊）
        _lightBrightView.layer.shadowOffset = CGSizeMake(0, 1); // 阴影偏移（0,0表示四周均匀）
        _lightBrightView.layer.masksToBounds = NO;
        _lightBrightView.layer.cornerRadius = 15;
        _lightBrightView.clipsToBounds = NO;
    }
    return _lightBrightView;
}


-(UIView *)lightModeView{
    if (!_lightModeView) {
        _lightModeView = [[UIView alloc]initWithFrame:CGRectMake(20, CGRectGetMaxY(self.lightBrightView.frame)+30, KScreenWidth-40, 120)];
        _lightModeView.backgroundColor = RGB(239, 240, 249);
        _lightModeView.layer.shadowOpacity = 0.5;          // 阴影透明度（0~1）
        _lightModeView.layer.shadowColor = [UIColor grayColor].CGColor; // 阴影颜色
        _lightModeView.layer.shadowRadius = 1;             // 阴影扩散范围（值越大越模糊）
        _lightModeView.layer.shadowOffset = CGSizeMake(0, 1); // 阴影偏移（0,0表示四周均匀）
        _lightModeView.layer.masksToBounds = NO;
        _lightModeView.layer.cornerRadius = 15;
        _lightModeView.clipsToBounds = NO;
    }
    return _lightModeView;
}

-(UILabel *)speedLabel{
    if (!_speedLabel) {
        _speedLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 16, CGRectGetWidth(self.sliderView.frame)-40, 20)];
        _speedLabel.font = [UIFont systemFontOfSize:17];
        _speedLabel.textColor = RGB(17, 17, 17);
        _speedLabel.text = [NSString stringWithFormat:@"%@%@%@%@",LocalString(@"风扇速度"),LocalString(@"："),@"0",LocalString(@"档")];
    }
    return _speedLabel;
}

-(UILabel *)modeLabel{
    if (!_modeLabel) {
        _modeLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 16, CGRectGetWidth(self.lightModeView.frame)-40, 20)];
        _modeLabel.font = [UIFont systemFontOfSize:17];
        _modeLabel.textColor = RGB(17, 17, 17);
        _modeLabel.text = [NSString stringWithFormat:@"%@",LocalString(@"灯光模式")];
    }
    return _modeLabel;
}

-(UILabel *)brightLabel{
    if (!_brightLabel) {
        _brightLabel = [[UILabel alloc]initWithFrame:CGRectMake(20, 16, CGRectGetWidth(self.lightBrightView.frame)-40, 20)];
        _brightLabel.font = [UIFont systemFontOfSize:17];
        _brightLabel.textColor = RGB(17, 17, 17);
        _brightLabel.text = [NSString stringWithFormat:@"%@",LocalString(@"主灯亮度")];
    }
    return _brightLabel;
}

-(NSMutableArray *)dotViews{
    if (!_dotViews) {
        _dotViews = [[NSMutableArray alloc]init];
    }
    return _dotViews;
}

-(UIView *)brightListView{
    if (!_brightListView) {
        _brightListView = [[UIView alloc]initWithFrame:CGRectMake(20, 53, CGRectGetWidth(self.lightBrightView.frame)-40, 44)];
        _brightListView.backgroundColor = RGB(239, 240, 249);
        _brightListView.layer.cornerRadius = 22;
        _brightListView.layer.masksToBounds = YES;
        _brightListView.layer.borderWidth = 1;
        _brightListView.layer.borderColor = RGB(17, 17, 17).CGColor;
        _brightListView.clipsToBounds = YES;
    }
    return _brightListView;
}

@end
